<?php
namespace Common\Controller;
use Common\Controller\CommonController;
class NewIdiomController extends CommonController
{

    /**
     * 缓存题目
     */
    public function setidiomquersion(){

        $question = M('newidiom_questions')->field('sort,createtime',true)->select();

        foreach ($question as $key=>$value){
            $question[$key]['imgsrc'] = $this->url.$question[$key]['imgsrc'];
        }

        S('newidiom_questions',$question,3600*24*30);

    }

    /**
     * 获取题目缓存并返回随机值
     */
    public function getidiomquersion($num){

        $cache = S('newidiom_questions');

        if (!$cache||$cache==""){
            $this->setidiomquersion();
            $cache = S('newidiom_questions');
        }

        srand((float) microtime() * 10000000);
        $id = array_rand($cache,$num);
        $data = array();
        for ($i=0;$i<count($id);$i++){
            $data[] = $cache[$id[$i]];
        }

        return $data;
    }

    /**
     * 缓存本周排行
     */
    public function setcacheweek(){

        $year = date('Y');
        $week = (int)date('W');
        $info = M();

        $where = array(
            'week.year'=>$year,
            'week.week'=>$week,
        );

        $list =$info->table('mj_newidiom_week as week')->join('mj_newidiom_member as uesr on  week.uid = uesr.uid')->where($where)->field('week.getnum,week.idiom_week_id,uesr.nickname,uesr.avatarurl')->limit(100)->order('week.getnum desc' )->select();
//
//        if ($list) {
//            $gift = M('idiom_gift')->field('giftname')->order('rank asc')->select();
//
//            $list[0]['money'] = '预计'.$gift[0]['giftname'];
//            if ($list[1]['id']){
//                $list[1]['money'] = '预计'.$gift[1]['giftname'];
//            }
//            if ($list[2]['id']){
//                $list[2]['money'] = '预计'.$gift[2]['giftname'];
//            }
//
//            if ($list[3]['id']){
//                $list[3]['money'] = '预计'.$gift[3]['giftname'];
//            }
//
//            if ($list[4]['id']){
//                $list[4]['money'] = '预计'.$gift[3]['giftname'];
//            }
//
//            if ($list[5]['id']){
//                $list[5]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//            if ($list[6]['id']){
//                $list[6]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//            if ($list[7]['id']){
//                $list[7]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//            if ($list[8]['id']){
//                $list[8]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//            if ($list[9]['id']){
//                $list[9]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//
//        }

        S('newidiom_week',$list,600);

    }

    /**
     * 缓存本周排行(版本2)
     */
    public function setcacheweek2(){

        $year = date('Y');
        $week = (int)date('W');
        $info = M();

        $where = array(
            'week.year'=>$year,
            'week.week'=>$week,
        );

        $list =$info->table('mj_newidiom_week as week')->join('mj_newidiom_member as uesr on  week.uid = uesr.uid')->where($where)->field('week.customs,week.id,uesr.nickname,uesr.avatarurl')->limit(100)->order('week.customs desc' )->select();


        S('newidiomweekt',$list,600);

    }

    /**
     * 获取本周排行版本2
     * @return mixed|string
     */
    public function getcacheweek2(){

        $cache = "";
        $cache = S('newidiomweekt');

        if (!$cache||$cache==""){
            $this->setcacheweek2();
            $cache = S('newidiomweekt');
        }
        return $cache;
    }


    /**
     * 获取本周排行
     * @return mixed|string
     */
    public function getcacheweek(){

        $cache = "";
        $cache = S('newidiomweek');

        if (!$cache||$cache==""){
            $this->setcacheweek();
            $cache = S('newidiomweek');
        }
        return $cache;
    }

    /**
     * 查询上周得奖
     */
    public function setlasetweek(){

        $lastweek = (int)date("W", strtotime("-1 week"));;

        $week = date('W');

        $year = date('Y');
        if ($week=='1'){
            $year= date("Y", strtotime("-1 year"));
        }


        $info = M();

        $where = array(
            'week.year'=>$year,
            'week.week'=>$lastweek,
        );

        $list =$info->table('mj_newidiom_week as week')->join('mj_newidiom_member as uesr on  week.uid = uesr.uid')->where($where)->field('week.getnum,week.idiom_week_id,uesr.nickname,uesr.avatarurl')->limit(10)->order('week.getnum desc' )->select();

        if ($list) {
            $gift = M('idiom_gift')->field('giftname')->order('rank asc')->select();

            $list[0]['giftname'] = $gift[0]['giftname'];
            if ($list[1]['id']){
                $list[1]['giftname'] = $gift[1]['giftname'];
            }
            if ($list[2]['id']){
                $list[2]['giftname'] = $gift[2]['giftname'];
            }

            if ($list[3]['id']){
                $list[3]['giftname'] = $gift[3]['giftname'];
            }

            if ($list[4]['id']){
                $list[4]['giftname'] = $gift[3]['giftname'];
            }

           if ($list[5]['id']){
               $list[5]['giftname'] = $gift[4]['giftname'];
           }

            if ($list[6]['id']){
                $list[6]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[7]['id']){
                $list[7]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[8]['id']){
                $list[8]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[9]['id']){
                $list[9]['giftname'] = $gift[4]['giftname'];
            }


        }

        S('lastweek',$list,3600);

    }

    /**
     * 获取上周得奖
     * @return mixed
     */
    public function getlastweek(){

        $cache = S('lastweek');

        if (!$cache||$cache==""){
            $this->setlasetweek();
            $cache = S('lastweek');
        }

        return $cache;
    }


    public function monilastweek(){

        $gift = M('newidiom_gift')->field('giftname')->order('rank asc')->select();
        $a1 = array(
            'nickname'=>'小时代',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/8R3rKudsCiciaecdzByO52qxEF9IVrul9yfrdYa7ibmnyhmedxDcdLfYA5FIC9zaDic8ReIsIoJu1NcWnaZYFYFFZg/0',
            'giftname'=>$gift[0]['giftname'],
        );

        $a2 = array(
            'nickname'=>'你好，生活',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/cC0d9lAQ3SI4JJyd3YRoDyCAia4oJwwabGFF3rZDotlGZofKyfptQvcSEz5MEwo2W4AMFVlhXkHvdGiaoSK0oDJA/0',
            'giftname'=>$gift[1]['giftname'],
        );
        $a3 = array(
            'nickname'=>'阿西吧',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLD8ia3yWyiaVcw6wzk9fMeoBcmK4kqRlnNEfyWPUTp05QVvicFWPvO9HYVDs6bzbjfWSDYr3sZsMJNg/0',
            'giftname'=>$gift[2]['giftname'],
        );
        $a4 = array(
            'nickname'=>'生活百态',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/BhKI1CuHnjxHRcuHK5tspzHG7nv8xQfJaKcTxfpoASfDBLQLgdAZTuIqiayDcJpndZJj2ntwibhejStHugHD9ZAw/0',
            'giftname'=>$gift[3]['giftname'],
        );
        $a5 = array(
            'nickname'=>'asdvs',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/0YRSb7hiaGZfYU1zVWg8QUhRnMq7twb2qZVwibs2f4jD2via98bLEcfXEYnN3j5CFRM2U1U4KlxiaiboQugU8VxoSzA/0',
            'giftname'=>$gift[3]['giftname'],
        );
        $a6 = array(
            'nickname'=>'人生如梦',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/gtBia22uicroVwMt22LiamAwwnvzlKggT0Nawd5X6OWrQOCOaDF8w3kngJsRNCnVODqmTuwdk4uficMG1ZiaibjjwC3Q/0',
            'giftname'=>$gift[4]['giftname'],
        );

        $a7 = array(
            'nickname'=>'过往.....',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJPFdtUib2BQHfvB9hfzHKib4hUsa6b0hky1JPwT7Y8UZic4Voevnky45U9XBj4SkSofEhtRiaSoicxkPQ/0',
            'giftname'=>$gift[4]['giftname'],
        );
        $a8 = array(
            'nickname'=>'依然记得',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83erYibdVmPXiblHv0z4ZPU4d9Xm3MPulWejtmhNQhiahLiaxaNJF0p4oIfBYPWXsu2zAwLqRskscC7h31A/0',
            'giftname'=>$gift[4]['giftname'],
        );
        $a9 = array(
            'nickname'=>'流浪.......',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTISJHoK6dDib7A7MP2dhibc7dia4V873OIwfgE285AHOv3gzlD69MF99p9Img7THqzo370GQUNsOfYcg/0',
            'giftname'=>$gift[4]['giftname'],
        );
        $a10 = array(
            'nickname'=>'iuriik',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/HiaasQla0F1YrAUZB7zp9giaUib9jaSEMK4icVDhdiaWx2UFBJbvHH5BAsAsQs35RbdiaX3UgTAmzUST8jjYTXWjkpOQ/0',
            'giftname'=>$gift[4]['giftname'],
        );

        $data = array(
            '0'=>$a1,
            '1'=>$a2,
            '2'=>$a3,
            '3'=>$a4,
            '4'=>$a5,
            '5'=>$a6,
            '6'=>$a7,
            '7'=>$a8,
            '8'=>$a9,
            '9'=>$a10,
        );

        return $data;
    }

    /**
     * 获取当天排行
     */
    public function getrank($date,$state){

        $cache = S('newdate_rank');
        if (!empty($cache)){
           // return $cache;
        }
        if (empty($date)){
            $date = date('Y-m-d');
        }

        $date = '2018-06-06';
        $ids = M('newidiom_chang')->where(array('date'=>$date))->getField('uid',true);
        $data = array();
        if (!empty($ids)){
            $ids = array_unique($ids);  //去除重复值
            $ids = array_merge($ids);  //去下标
            for ($i=0;$i<count($ids);$i++){

                $where['date'] = $date;
                $where['uid'] = $ids[$i];
                $info = M('newidiom_chang')->field('id,uid',true)->where($where)->order('customs desc')->find();
                $data[] = $info;
            }
            //冒泡
            $len = count($data);
            for ($k=0;$k<$len;$k++){
                for ($j=0;$j<$len-$k;$j++){
                    if ($data[$j]['customs']<$data[$j+1]['customs']){
                        $tem = $data[$j];
                        $data[$j] = $data[$j+1];
                        $data[$j+1] = $tem;
                    }
                }

            }
            if ($state=='2'){

                if ($data[0]){
                    $data[0]['giftname'] = '获得奖金100元';
                }
                if ($data[1]){
                    $data[1]['giftname'] = '获得奖金50元';
                }
                if ($data[2]){
                    $data[2]['giftname'] = '获得奖金30元';
                }

                $arr['gift'] = array(
                    '0'=>$data[0],
                    '1'=>$data[1],
                    '2'=>$data[2],
                );
                $m = date('m',strtotime($date));
                $d = date('d',strtotime($date));
                $arr['date'] = (int)$m.'月'.(int)$d.'日';
                return $arr;
            }

            if ($data[0]){
                $data[0]['giftname'] = '预计获得奖金100元';
            }
            if ($data[1]){
                $data[1]['giftname'] = '预计获得奖金50元';
            }
            if ($data[2]){
                $data[2]['giftname'] = '预计获得奖金30元';
            }


        }

        S('newdate_rank',$data,600);
        return S('newdate_rank');

    }

    /**
     * 近期得奖
     */
    public function gift(){

        $date1 = '2018-06-17';
        $date2 = '2018-06-18';
        $date3 = '2018-06-19';
        $state = 2;
        $data[0] = $this->getrank($date1,$state);
        $data[1] = $this->getrank($date2,$state);
        $data[2] = $this->getrank($date3,$state);

        return $data;

    }


    /**
     * 生成红包
     */
    public function create_redbag($uid)
    {
        $where_relationship                 =   array();
        $field                              =   array('id');
        $where_relationship['my_member_id'] =   $uid;
        $is_shere                           =   M('newidiom_relationship')->where($where_relationship)->field($field)->find();

        $red_total                  =   $is_shere ? rand(10,15) / 100  :  rand(10,13) / 100; //奖金
        $insert_data                =   array();
        $insert_data['uid']         =   $uid;
        $insert_data['red_total']   =   number_format($red_total, 2);
        $insert_data['status']      =   0;
        $insert_data['create_time'] =   date('Y-m-d H:i:s');
        $insert_data['total_sn']    =   $this->create_order();
        $insert_data['time']        =   time();

        $result     =    M('newidiom_redbag')->add($insert_data);
        if (! empty($result))
        {
            $insert_data['challenge_id']    =   $result;
            $this->create_redbag_log($insert_data);
            $this->create_change_log($insert_data);
        }
        return  $result;
    }

    //生成红包记录
    private function create_redbag_log($insert_data)
    {
        $insert_data['create_time'] =   date('Y-m-d H:i:s');
        $insert_data['time']        =   time();
        M('newidiom_redbag_log')->add($insert_data);
    }

    //生成收支记录
    private  function create_change_log($insert_data)
    {
        //添加一条收入记录
        $insert_change_log                  =   array();
        $insert_change_log['game_id']       =   1;
        $insert_change_log['uid']           =   $insert_data['uid'];
        $insert_change_log['red_total']     =   '+'.$insert_data['red_total'];
        $insert_change_log['status']        =   0;
        $insert_change_log['create_time']   =   $insert_data['create_time'];
        $insert_change_log['from_sn']       =   $insert_data['total_sn'];
        $insert_change_log['type_id']       =   1;
        $insert_change_log['status_text']   =   '现金红包';
        $insert_change_log['time']          =   time();
        M('newidiom_change_log')->add($insert_change_log);
    }

    /**
     * 生成订单
     */
    private function create_order()
    {
        $order_id           =   'HB'.date('YmdHis').rand(100000,999999);
        $where              =   array();
        $field              =   array('challenge_id');
        $where['total_sn']  =   $order_id;
        $resutl             =   M('newidiom_redbag')->where($where)->field($field)->find();
        if (empty($resutl))
        {
            return  $order_id;
        }
        else
        {
            $this->create_order();
        }
    }
}